//
//  QCRulerController.swift
//  爱汽车
//
//  Created by liujianlong on 2023/8/24.
//

import UIKit

class QCRulerController: QCViewController {
    var array = ["左前传感器","右前传感器","左后传感器","右后传感器"]
    var values : [Int] = [0,0,0,0]
    var limit = (max:100,min:0)
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
    }
    
    override func initUI() {
        title = LS("高度尺工具")
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(QCHeightRulerCell.nib(), forCellReuseIdentifier: QCHeightRulerCell.ID())
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    override var base_info: [UInt8]{
        didSet{
            ///必须要强转一下，不然数据不对
            let data = [UInt8](base_info[AI_index..<base_info.count])
            values = data[16...19].map{Int($0)}
            tableView.reloadData()
        }
    }
    
    func action(_ value:Int, _ row:Int){
        values[row] = value
        let str = values.map{String(format: "%.2X", UInt8($0))}.joined()
        BLEData.appSetDataToPer(AIQC.manualHightPre.cmd, str).write
    }
}
//MARK: - UITableViewDataSource
extension QCRulerController:UITableViewDataSource
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: QCHeightRulerCell.ID()) as! QCHeightRulerCell
        cell.title.text = LS(array[indexPath.row])
        cell.limit = limit
        cell.value = values[indexPath.row]
        cell.block = {[weak self] value in
            self?.action(value,indexPath.row)
        }
        return cell
    }
}
//MARK: - UITableViewDelegate
extension QCRulerController:UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
}
